package com.chuang.bootplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chuang.bootplus.base.database.EntityBase;
import com.chuang.bootplus.base.exception.BusException;
import com.chuang.bootplus.base.utils.ApiResponse;
import com.chuang.bootplus.base.utils.BeanUtil;
import com.chuang.bootplus.entity.Hotnews;
import com.chuang.bootplus.entity.Love;
import com.chuang.bootplus.entity.User;
import com.chuang.bootplus.mapper.HotnewsMapper;
import com.chuang.bootplus.po.HotnewsPO;

import com.chuang.bootplus.po.LovePO;
import com.chuang.bootplus.po.NewsAddPO;
import com.chuang.bootplus.service.HotnewsService;
import com.chuang.bootplus.service.LoveService;
import com.chuang.bootplus.service.UserService;
import com.chuang.bootplus.vo.HotNewsVO;
import java.lang.invoke.SerializedLambda;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HotnewsServiceImpl extends ServiceImpl<HotnewsMapper, Hotnews> implements HotnewsService {

    @Autowired
    LoveService loveService;

    public ApiResponse<HotNewsVO> getnews(HotnewsPO po) {
        Page<Hotnews> pages = new Page(po.getPageNumber(), po.getPageSize());
        Page<Hotnews> page = page(pages, new LambdaQueryWrapper());
        List<HotNewsVO> hotNewsVOS = BeanUtil.listA2ListB(page.getRecords(), HotNewsVO.class);
        hotNewsVOS.stream().forEach(t -> {
            System.out.println(t.getId());
            if (loveService.getOne(new LambdaQueryWrapper<Love>().eq(Love::getUserId,po.getUserId()).eq(Love::getHotnewsId,t.getId())) == null){
                System.out.println("null is"+t.getId());
                t.setIslove(false);
            }else {
                System.out.println("not null is"+t.getId());
                t.setIslove(true);
            }
        });
        return new ApiResponse(hotNewsVOS);
    }

    public ApiResponse<Void> addnews(NewsAddPO po) {
        if (getOne(new LambdaQueryWrapper<Hotnews>().eq(Hotnews::getId, po.getId())) != null){
            throw new BusException("新闻已存在");
        }
        Hotnews hotnews = BeanUtil.beanA2beanB(po, Hotnews.class);
        save(hotnews);
        return new ApiResponse();
    }

    public ApiResponse<HotNewsVO> getnewsById(HotnewsPO po) {
        Hotnews one = getOne(new LambdaQueryWrapper<Hotnews>().eq(Hotnews::getId, po.getId()));
        if (one == null){
            throw new BusException("新闻已存在");
        }

        HotNewsVO hotNewsVO = BeanUtil.beanA2beanB(one, HotNewsVO.class);
        return new ApiResponse(hotNewsVO);
    }

    @Autowired
    UserService userService;

    @Override
    public ApiResponse<HotNewsVO> gethostlove() {
        Page pages=new Page(1,6);
        Page page=loveService.page(pages,new LambdaQueryWrapper<Love>().isNotNull(Love::getHotnewsId));
        //getRecord 获得所有
        List<Love> loveList= BeanUtil.listA2ListB(page.getRecords(),Love.class);

        Map<Long,Long> map =loveList.stream().collect(Collectors.groupingBy(Love::getHotnewsId,Collectors.counting()));
        List<Long> collect = map.entrySet().stream().sorted((k1, k2) ->
                -k1.getValue().compareTo(k2.getValue())).map(k1 -> k1.getKey()).collect(Collectors.toList());
        LinkedList<Hotnews> hotlist = new LinkedList<>();
        collect.forEach(t->{
            System.out.println(t);
            if(getOne(new LambdaQueryWrapper<Hotnews>().eq(Hotnews::getId, t))!=null) {
                hotlist.add(getOne(new LambdaQueryWrapper<Hotnews>().eq(Hotnews::getId, t)));
            }
        });
        List<HotNewsVO> hotNewsVOS = BeanUtil.listA2ListB(hotlist, HotNewsVO.class);
        return new ApiResponse<>(hotNewsVOS);
    }

    public ApiResponse<Void> deletelove(LovePO po) {

        Love one3 = loveService.getOne(new LambdaQueryWrapper<Love>().eq(Love::getHotnewsId, po.getNewsId()).eq(Love::getUserId, po.getUserId()));
        if (one3 == null){
            throw new BusException("新闻不存在");
        }
        Hotnews one = getOne(new LambdaQueryWrapper<Hotnews>().eq(Hotnews::getId, po.getNewsId()));

        if (one == null){
            throw new BusException("新闻不存在");
        }
        one.setLovenum(one.getLovenum() - 1);
        saveOrUpdate(one);
        loveService.remove(new LambdaQueryWrapper<Love>().eq(Love::getHotnewsId, po.getNewsId()).eq(Love::getUserId, po.getUserId()));
        return new ApiResponse();
    }



    public ApiResponse<Void> love(LovePO po) {
        Hotnews one =getOne(new LambdaQueryWrapper<Hotnews>().eq(Hotnews::getId, po.getNewsId()));
        if (one == null){
            throw new BusException("新闻不存在");
        }
        User one1 = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getId, po.getUserId()));
        if (one == null){
            throw new BusException("用户不存在");
        }
        one.setLovenum(Integer.valueOf(one.getLovenum().intValue() + 1));
        saveOrUpdate(one);
        Love love = BeanUtil.beanA2beanB(po, Love.class);
        love.setUserId(po.getUserId());
        love.setHotnewsId(po.getNewsId());
        loveService.save(love);
        HotNewsVO hotNewsVO = BeanUtil.beanA2beanB(one, HotNewsVO.class);
        return new ApiResponse();
    }



}
